<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body style="width: 100%; height: 100%">
<div id="chessDiv" style="position: absolute;top: 0px;left: 0px;bottom: 0px;right: 0px">
    <canvas id="chessCanvas"></canvas>
</div>
</body>

<script>
    var canvasDom = document.getElementById('chessCanvas');
    var divDom = document.getElementById('chessDiv');
    canvasDom.width = divDom.clientWidth;
    canvasDom.height = divDom.clientHeight;

    var ctx = canvasDom.getContext('2d');

    var chessboardX = 100;
    var chessboardY = 100;

    var VerticalLineCount = 15;
    var HorizontalLineCount = 15;

    var cellWidth = 20;

    var chessboardWidth = (VerticalLineCount-1)*cellWidth;
    var chessboardHeight = (HorizontalLineCount-1)*cellWidth;

    var curChessData = [];
    for(var i=0; i<HorizontalLineCount; i++){
        for(var j=0; j<VerticalLineCount; j++){
            curChessData.push(0);
        }
    }

    function drawOneChess(x,y,data) {
        if(data==0){
            return;
        }else if(data==1){
            ctx.fillStyle='#000000';
        }else if(data==-1){
            ctx.fillStyle='#aaaaaa';
        }
        ctx.beginPath();
        ctx.arc(chessboardX+x*cellWidth,chessboardY+y*cellWidth,cellWidth/2,0,2*Math.PI);
        ctx.fill();
    }

    function drawAll(chessData, lastXY) {
        ctx.fillStyle='#8ABAFF';
        ctx.fillRect(chessboardX,chessboardY,chessboardWidth,chessboardHeight);

        ctx.fillStyle='#000000';
        for(var i=0; i<HorizontalLineCount; i++){
            ctx.fillRect(chessboardX,chessboardY+i*cellWidth,chessboardWidth,1);
            ctx.fillRect(chessboardX+i*cellWidth,chessboardY,1,chessboardHeight);
        }

        for(var y=0; y<HorizontalLineCount; y++){
            for(var x=0; x<VerticalLineCount; x++){
                drawOneChess(x,y,chessData[y*VerticalLineCount+x]);
            }
        }
		
		if(lastXY){
			ctx.fillStyle='#ff0000';
			ctx.fillRect(chessboardX+lastXY[0]*cellWidth-1,chessboardY+lastXY[1]*cellWidth-1,3,3);
		}
		
    }

    function getScoreByBAndW(blackCount, whiteCount) {
        var curScore = 0;

        if(blackCount>0&&whiteCount>0){
            curScore = 0;
        }else if(blackCount==0&&whiteCount==0){
            curScore = 10;
        }else{
            if(blackCount>0){
                if(blackCount == 1){
                    curScore = -100;
                }else if(blackCount == 2){
                    curScore = -1000;
                }else if(blackCount == 3){
                    curScore = -10000;
                }else if(blackCount == 4){
                    curScore = -100000;
                }else if(blackCount == 5){
                    curScore = -Infinity;
                }
            }else{
                if(whiteCount == 1){
                    curScore = 10;
                }else if(whiteCount == 2){
                    curScore = 100;
                }else if(whiteCount == 3){
                    curScore = 1000;
                }else if(whiteCount == 4){
                    curScore = 10000;
                }else if(whiteCount == 5){
                    curScore = Infinity;
                }
            }
        }

        return curScore;
    }

    function getScoreByChessData(chessData){
        var score = 0;

        for(var y0=0; y0<HorizontalLineCount; y0++){
            for(var x0=0; x0<=VerticalLineCount-5; x0++){

                var blackCount = 0;
                var whiteCount = 0;
                for(var x=x0; x<x0+5; x++){
                    if(chessData[y0*VerticalLineCount+x] == 1){
                        blackCount++;
                    }else if(chessData[y0*VerticalLineCount+x] == -1){
                        whiteCount++;
                    }
                }
                var curScore = getScoreByBAndW(blackCount, whiteCount);

                score += curScore;
            }
        }

        for(var y0=0; y0<=HorizontalLineCount-5; y0++){
            for(var x0=0; x0<VerticalLineCount; x0++){

                var blackCount = 0;
                var whiteCount = 0;
                for(var y=y0; y<y0+5; y++){
                    if(chessData[y*VerticalLineCount+x0] == 1){
                        blackCount++;
                    }else if(chessData[y*VerticalLineCount+x0] == -1){
                        whiteCount++;
                    }
                }
                var curScore = getScoreByBAndW(blackCount, whiteCount);

                score += curScore;
            }
        }

        for(var y0=0; y0<=HorizontalLineCount-5; y0++){
            for(var x0=0; x0<=VerticalLineCount-5; x0++){

                var blackCount = 0;
                var whiteCount = 0;
                for(var offset=0; offset<5; offset++){
                    if(chessData[(y0+offset)*VerticalLineCount+(x0+offset)] == 1){
                        blackCount++;
                    }else if(chessData[(y0+offset)*VerticalLineCount+(x0+offset)] == -1){
                        whiteCount++;
                    }
                }
                var curScore = getScoreByBAndW(blackCount, whiteCount);

                score += curScore;
            }
        }

        for(var y0=0; y0<=HorizontalLineCount-5; y0++){
            for(var x0=4; x0<VerticalLineCount; x0++){

                var blackCount = 0;
                var whiteCount = 0;
                for(var offset=0; offset<5; offset++){
                    if(chessData[(y0+offset)*VerticalLineCount+(x0-offset)] == 1){
                        blackCount++;
                    }else if(chessData[(y0+offset)*VerticalLineCount+(x0-offset)] == -1){
                        whiteCount++;
                    }
                }
                var curScore = getScoreByBAndW(blackCount, whiteCount);

                score += curScore;
            }
        }

        return score;
    }

//    function getScoreByChessData(chessData, x, y){
//        var score = 0;
//
//        for(var offsetHead=0,xHead=x-4,yHead=y; offsetHead<5; offsetHead++,xHead=x-4+offsetHead){
//
//            if(xHead<0||xHead+4>VerticalLineCount-1){
//               continue;
//            }
//
//            var blackCount = 0;
//            var whiteCount = 0;
//
//            for(var offset=0; offset<5; offset++){
//                if(chessData[yHead*VerticalLineCount+xHead+offset] == 1){
//                    blackCount++;
//                }else if(chessData[yHead*VerticalLineCount+xHead+offset] == -1){
//                    whiteCount++;
//                }
//            }
//            var curScore = getScoreByBAndW(blackCount, whiteCount);
//
//            score += curScore;
//        }
//
//
//        for(var offsetHead=0,xHead=x,yHead=y-4; offsetHead<5; offsetHead++,yHead=y-4+offsetHead){
//
//            if(yHead<0||yHead+4>HorizontalLineCount-1){
//                continue;
//            }
//
//            var blackCount = 0;
//            var whiteCount = 0;
//
//            for(var offset=0; offset<5; offset++){
//                if(chessData[(yHead+offset)*VerticalLineCount+xHead] == 1){
//                    blackCount++;
//                }else if(chessData[(yHead+offset)*VerticalLineCount+xHead] == -1){
//                    whiteCount++;
//                }
//            }
//            var curScore = getScoreByBAndW(blackCount, whiteCount);
//
//            score += curScore;
//        }
//
//        for(var offsetHead=0,xHead=x-4,yHead=y-4; offsetHead<5; offsetHead++,xHead=x-4+offsetHead,yHead=y-4+offsetHead){
//
//            if(xHead<0||yHead<0||xHead+4>VerticalLineCount-1||yHead+4>HorizontalLineCount-1){
//                continue;
//            }
//
//            var blackCount = 0;
//            var whiteCount = 0;
//
//            for(var offset=0; offset<5; offset++){
//                if(chessData[(yHead+offset)*VerticalLineCount+xHead+offset] == 1){
//                    blackCount++;
//                }else if(chessData[(yHead+offset)*VerticalLineCount+xHead+offset] == -1){
//                    whiteCount++;
//                }
//            }
//            var curScore = getScoreByBAndW(blackCount, whiteCount);
//
//            score += curScore;
//        }
//
//        for(var offsetHead=0,xHead=x+4,yHead=y-4; offsetHead<5; offsetHead++,xHead=x+4-offsetHead,yHead=y-4+offsetHead){
//
//            if(xHead>VerticalLineCount-1||yHead<0||xHead-4<0||yHead+4>HorizontalLineCount-1){
//                continue;
//            }
//
//            var blackCount = 0;
//            var whiteCount = 0;
//
//            for(var offset=0; offset<5; offset++){
//                if(chessData[(yHead+offset)*VerticalLineCount+xHead-offset] == 1){
//                    blackCount++;
//                }else if(chessData[(yHead+offset)*VerticalLineCount+xHead-offset] == -1){
//                    whiteCount++;
//                }
//            }
//            var curScore = getScoreByBAndW(blackCount, whiteCount);
//
//            score += curScore;
//        }
//
//        return score;
//    }

    function Node(param) {
        this.score = 0;
        this.xy = param.xy || [-1,-1];
        this.color = param.color;
        this.chessData = param.chessData;
        this.localScore = 0;

        this.floor = param.floor;

        this.brotherBestScore = null;
        this.bestChild = null;
        this.children = [];
        this.parent = param.parent;
        this.first = param.first;
    }

    Node.prototype.getLocalScore = function () {
        this.localScore = getScoreByChessData(this.chessData, this.xy[0], this.xy[1]);

        return this.localScore;
    };

    Node.prototype.getScore = function () {

        this.getLocalScore();
        if(this.localScore == Infinity){
            this.score = this.localScore;
            return this.score;
        }

        if(this.floor == 3){
            this.score = this.localScore;
        }else{

			var findCut = false;
            for(var y=0; y<HorizontalLineCount; y++){
				if(findCut){
				    break;
				}
                for(var x=0; x<VerticalLineCount; x++){

                    if(this.chessData[y*VerticalLineCount+x] != 0){
                        continue;
                    }

                    var isOK = false;

                    for(var y1=y-2; y1<=y+2; y1++){
                        if(isOK){
                            break;
                        }
                        if(y1<0||y1>HorizontalLineCount-1){
                            continue;
                        }
                        for(var x1=x-2; x1<=x+2; x1++){
                            if(x1<0||x1>VerticalLineCount-1){
                                continue;
                            }

                            if(this.chessData[y1*VerticalLineCount+x1] != 0){
                                isOK = true;
                                break;
                            }
                        }
                    }

                    if(this.first){
                        isOK = true;
                        x=7;
                        y=7;
                    }

                    debugger
                    if(isOK){
                        var xy = [x,y];
                        var color = -this.color;
                        var chessData = this.chessData.slice(0);
                        chessData[y*VerticalLineCount+x] = color;
                        var floor = this.floor+1;
                        var parent = this;

                        var child = new Node({
                            xy: xy,
                            color: color,
                            chessData: chessData,
                            floor: floor,
                            parent: parent
                        });

                        this.children.push(child);
                        child.getScore();
						
						if(this.children.length == 1){
							this.bestChild = child;
						}else{
							if(this.color == -1){
								if(child.score<this.bestChild.score){
									this.bestChild = child;
								}
							}else if(this.color == 1){
								if(child.score>this.bestChild.score){
									this.bestChild = child;
								}
							}
						}
						
						if(this.parent && this.parent.children.length>1){
							if(this.color == 1){
								if(child.score>this.parent.bestChild.score){
									findCut = true;
									break;
								}
							}else if(this.color == -1){
								if(child.score<this.parent.bestChild.score){
									findCut = true;
									break;
								}
							}
						}

                    }

                    if(this.first){
                        break;
                    }

                }
                if(this.first){
                    break;
                }
            }
			
			this.score = this.bestChild.score;

        }

        return this.score;
    };

//    function Tree(node) {
//        this.rootNode = node || new Node();
//        this.depth = 3;
//    }
//
//    Tree.prototype.getAnswer = function () {
//        this.rootNode.getScore();
//    };

    drawAll(curChessData);

    var node = new Node({
        xy: [-1,-1],
        color: -1,
        chessData: curChessData,
        floor: 0,
        parent: null,
        first: true,
    });
	node.getScore();
	var x = node.bestChild.xy[0];
	var y = node.bestChild.xy[1];
	curChessData[y*VerticalLineCount+x] = 1;
	drawAll(curChessData, node.bestChild.xy);

    canvasDom.addEventListener('click', function (e) {
        var x = Math.round((e.clientX - chessboardX)/chessboardWidth*(VerticalLineCount-1));
        var y = Math.round((e.clientY - chessboardY)/chessboardHeight*(HorizontalLineCount-1));

        if(x<0||x>VerticalLineCount-1||y<0||y>HorizontalLineCount-1||curChessData[y*VerticalLineCount+x]!=0){
            return;
        }
        curChessData[y*VerticalLineCount+x] = -1;
        drawAll(curChessData, [x,y]);
        var node = new Node({
            xy: [x,y],
            color: -1,
            chessData: curChessData,
            floor: 0,
            parent: null,
        });
		
		setTimeout(function(){
			node.getScore();
			
			var x = node.bestChild.xy[0];
			var y = node.bestChild.xy[1];
			
			curChessData[y*VerticalLineCount+x] = 1;
			drawAll(curChessData, node.bestChild.xy);
		},0);


    });


</script>

</html>